package com.mlh.controller;

import com.mlh.entity.Monster;
import com.mlh.mlhspringmvc.annotation.*;
import com.mlh.service.MonsterService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
public class MonsterController {
    @AutoWired
    private MonsterService monsterService;

    // 编写方法,可以列出妖怪列表
    // springmvc是支持原生的servlet api,为了看到底层机制
    // 这里我们设计两个参数
    @RequestMapping("/monster/list")
    public void listMonster(HttpServletRequest request, HttpServletResponse response) {
        // 设置编码和返回类型
        response.setContentType("text/html;charset=utf-8");
        StringBuilder content = new StringBuilder(" <h1>妖怪列表信息</h1>");
        // 调用monsterService
        List<Monster> monsters = monsterService.listMonster();
        content.append("<tabLe border=' 1px' width= ' 500px' styLe= ' border-collapse :collapse'>");
        for (Monster monster : monsters) {
            content.append("<tr><td>" + monster.getId()
                    + "</td><td>" + monster.getName() + "</td><td>"
                    + monster.getSkill() + "</td><td>"
                    + monster.getAge() + "</td></tr>");
        }
        content.append("</table>");
        //获取writer返回信息
        try {
            PrintWriter writer = response.getWriter();
            writer.write(content.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/monster/find")
    public void findMonsterByName(HttpServletRequest request, HttpServletResponse response,
                                  @RequestParam("name")String monsterName) {
        // 设置编码和返回类型
        response.setContentType("text/html;charset=utf-8");
        StringBuilder content = new StringBuilder(" <h1>妖怪列表信息</h1>");
        // 调用monsterService
        List<Monster> monsters = monsterService.findMonsterByName(monsterName);
        content.append("<tabLe border=' 1px' width= '400px' styLe= ' border-collapse :collapse'>");
        for (Monster monster : monsters) {
            content.append("<tr><td>" + monster.getId()
                    + "</td><td>" + monster.getName() + "</td><td>"
                    + monster.getSkill() + "</td><td>"
                    + monster.getAge() + "</td></tr>");
        }
        content.append("</table>");
        //获取writer返回信息
        try {
            PrintWriter writer = response.getWriter();
            writer.write(content.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 处理妖怪登录的方法,返回要请求转发/重定向的字符串
    @RequestMapping("/monster/login")
    public String login(String monsterName,HttpServletRequest request,HttpServletResponse response) {
        System.out.println("接收到的monsterName="+monsterName);
        request.setAttribute("monsterName",monsterName);
        boolean b = monsterService.login(monsterName);
        if (b) {// 登录成功!
            return "forward:/login_ok.jsp";
        } else {// 登录失败
            return "forward:/login_error.jsp";
        }
    }

    //编写方法，返回json格式的数据

    @RequestMapping("/monster/list/json")
    public List<Monster> listMonsterByJson(HttpServletRequest request,HttpServletResponse response){
        List<Monster> monsters = monsterService.listMonster();
        return monsters;
    }
}
